package com.gongpingjialoan.bean;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.gongpingjialoan.data.CategoryData;
import com.gongpingjialoan.data.CityData;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.FileUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DataManager {
    static DataManager dataManager;

    private boolean isBrandSuccess, isModelSuccess, isHotBrandSuccess, isCitySuccess;

    private boolean isBrandloading, isModellaoding, isHotBrandlaoding, isCitylaoding;


    public void setBrandSuccess(boolean isBrandSuccess) {
        this.isBrandSuccess = isBrandSuccess;
    }


    public void setModelSuccess(boolean isModelSuccess) {
        this.isModelSuccess = isModelSuccess;
    }


    public void setHotBrandSuccess(boolean isHotBrandSuccess) {
        this.isHotBrandSuccess = isHotBrandSuccess;
    }


    public void setCitySuccess(boolean isCitySuccess) {
        this.isCitySuccess = isCitySuccess;
    }

    public void setIsBrandloading(boolean isBrandloading) {
        this.isBrandloading = isBrandloading;
    }

    public void setIsModellaoding(boolean isModellaoding) {
        this.isModellaoding = isModellaoding;
    }

    public void setIsHotBrandlaoding(boolean isHotBrandlaoding) {
        this.isHotBrandlaoding = isHotBrandlaoding;
    }

    public void setIsCitylaoding(boolean isCitylaoding) {
        this.isCitylaoding = isCitylaoding;
    }

    //获取数据加载成功的条件
    public boolean getLoadingDataSuccess() {
        if (isBrandSuccess && isModelSuccess && isHotBrandSuccess && isCitySuccess) {
            return true;
        }
        return false;
    }


    //判断是否在加载数据
    public boolean isLoading() {
        if (isBrandloading && isModellaoding && isHotBrandlaoding && isCitylaoding) {
            return true;
        }
        return false;
    }

    public static DataManager getInstance() {
        if (dataManager == null) {
            dataManager = new DataManager();
        }

        return dataManager;
    }

    public void getBrandData(final CategoryData mCategoryData, final String path, final Context context, final int time) {
        //if(isBrandSuccess)  return;
        isBrandloading = true;
        NetDataJson mNetBrandData = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                try {
                    JSONArray mBrandJson = json.getJSONArray("brand");
                    if (null != mBrandJson) {
                        mCategoryData.LoadBrandData(mBrandJson);
                    }

                    FileUtils file = new FileUtils();
                    file.write2SDFromBytes(path, "mobile_brands", mBrandJson.toString().getBytes());
                    isBrandSuccess = true;
                    SharedPreferences sharedPreferences = context.getSharedPreferences("db", Context.MODE_PRIVATE);
                    Editor edit = sharedPreferences.edit();
                    edit.putInt("local_brand_time", time);
                    edit.commit();
                    isBrandloading = false;
                } catch (JSONException e) {
                    isBrandSuccess = false;
                    isBrandloading = false;
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                isBrandSuccess = false;
            }
        });
        mNetBrandData.setUrl("/mobile/api/brand-data/");
        mNetBrandData.request("get");
    }

    public void getModelData(final CategoryData mCategoryData, final String path, final Context context, final int time) {
        //if(isModelSuccess)  return;
        isModellaoding = true;
        NetDataJson mNetBrandData = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                try {
                    JSONArray Json = json.getJSONArray("model");

                    if (null != Json) {
                        mCategoryData.LoadModelData(Json);
                    }

                    FileUtils file = new FileUtils();
                    file.write2SDFromBytes(path, "mobile_models", Json.toString().getBytes());
                    isModelSuccess = true;
                    SharedPreferences sharedPreferences = context.getSharedPreferences("db", Context.MODE_PRIVATE);
                    Editor edit = sharedPreferences.edit();
                    edit.putInt("local_model_time", time);
                    edit.commit();
                    isModellaoding = false;
                } catch (JSONException e) {
                    isModelSuccess = false;
                    isModellaoding = false;
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                isModelSuccess = false;
            }
        });
        mNetBrandData.setUrl("/mobile/api/model-data/");
        mNetBrandData.request("get");
    }

    public void getHotBrandData(final CategoryData mCategoryData, final String path, final Context context,
                                final int time) {
        //if(isHotBrandSuccess)  return;
        isHotBrandlaoding = true;
        NetDataJson mNetBrandData = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                try {
                    JSONArray Json = json.getJSONArray("hot_brand");

                    if (null != Json) {
                        mCategoryData.LoadHotBrandData(Json);
                    }

                    FileUtils file = new FileUtils();
                    file.write2SDFromBytes(path, "mobile_hot_brands", Json.toString().getBytes());
                    isHotBrandSuccess = true;
                    SharedPreferences sharedPreferences = context.getSharedPreferences("db", Context.MODE_PRIVATE);
                    Editor edit = sharedPreferences.edit();
                    edit.putInt("local_hot_time", time);
                    edit.commit();
                    isHotBrandlaoding = false;
                } catch (JSONException e) {
                    isHotBrandSuccess = false;
                    isHotBrandlaoding = false;
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                isHotBrandSuccess = false;
            }
        });
        mNetBrandData.setUrl("/mobile/api/hot-brand/");
        mNetBrandData.request("get");
    }

    public void getCityData(final CityData mCityData, final String path, final Context context, final int time) {
        //if(isCitySuccess)  return;
        isCitylaoding = true;
        NetDataJson mNetBrandData = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                try {
                    JSONArray Json = json.getJSONArray("cities");

                    if (null != Json) {
                        mCityData.LoadCityData(Json);
                        mCityData.LoadCityData1(Json);
                    }

                    FileUtils file = new FileUtils();
                    file.write2SDFromBytes(path, "city", Json.toString().getBytes());
                    isCitySuccess = true;
                    SharedPreferences sharedPreferences = context.getSharedPreferences("db", Context.MODE_PRIVATE);
                    Editor edit = sharedPreferences.edit();
                    edit.putInt("local_city_time", time);
                    edit.commit();
                    isCitylaoding = false;
                } catch (JSONException e) {
                    isCitySuccess = false;
                    isCitylaoding = false;
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                isCitySuccess = false;
            }
        });
        mNetBrandData.setUrl("/mobile/api/city-data/");
        mNetBrandData.request("get");
    }
}
